{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试集数据预处理\n",
    "这一部分的过程与训练集的数据处理基本一致，这里不再赘述。有区别的地方在于可能官方给的测试集的字段与训练集不完全一致，所以使用本部分代码时需要注意"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import datetime\n",
    "\n",
    "# 获取当前的工作目录\n",
    "pwd = os.getcwd()\n",
    "# 将工作目录更改到测试集\n",
    "os.chdir(\"原始测试集\")\n",
    "# ——————————————————————————读取数据—————————————————————————————— #\n",
    "# 航班数据\n",
    "flight_data = pd.read_csv('flight.csv',sep=',',encoding='gb2312')\n",
    "# 天气数据\n",
    "weather = pd.read_excel('weather.xlsx')\n",
    "# 城市与机场对应数据\n",
    "airport_city = pd.read_excel('airport_city.xlsx')\n",
    "# 特情\n",
    "spcial = pd.read_excel('spcial.xlsx')\n",
    "# 天气情况\n",
    "case = pd.read_csv('weather_case.csv',encoding='gb2312')\n",
    "# 改回原来的工作目录\n",
    "os.chdir(pwd)\n",
    "flight_data['标识'] = flight_data['验证标识（1：需要选手预测；0：提前两小时航班信息参考数据）']\n",
    "del(flight_data['验证标识（1：需要选手预测；0：提前两小时航班信息参考数据）'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 纠正部分字段错误，没错误不需要纠正"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "flight_data['航班编号1'] = flight_data['飞机编号']\n",
    "flight_data['飞机编号'] = flight_data['航班编号']\n",
    "flight_data['航班编号'] = flight_data['航班编号1']\n",
    "del(flight_data['航班编号1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出发机场</th>\n",
       "      <th>到达机场</th>\n",
       "      <th>飞机编号</th>\n",
       "      <th>计划出发时间</th>\n",
       "      <th>计划到达时间</th>\n",
       "      <th>实际出发时间</th>\n",
       "      <th>实际到达时间</th>\n",
       "      <th>航班编号</th>\n",
       "      <th>取消标识</th>\n",
       "      <th>标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XIY</td>\n",
       "      <td>CKG</td>\n",
       "      <td>2403.0</td>\n",
       "      <td>1501545000</td>\n",
       "      <td>1501551000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MU2261</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>PVG</td>\n",
       "      <td>KMG</td>\n",
       "      <td>1126.0</td>\n",
       "      <td>1501544400</td>\n",
       "      <td>1501556400</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>CZ3677</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SJW</td>\n",
       "      <td>NKG</td>\n",
       "      <td>448.0</td>\n",
       "      <td>1501545300</td>\n",
       "      <td>1501551300</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9C8939</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>XIY</td>\n",
       "      <td>NKG</td>\n",
       "      <td>779.0</td>\n",
       "      <td>1501545300</td>\n",
       "      <td>1501552200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MU2387</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>TAO</td>\n",
       "      <td>PVG</td>\n",
       "      <td>948.0</td>\n",
       "      <td>1501543800</td>\n",
       "      <td>1501548900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MU5573</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  出发机场 到达机场    飞机编号      计划出发时间      计划到达时间  实际出发时间  实际到达时间    航班编号 取消标识  标识\n",
       "0  XIY  CKG  2403.0  1501545000  1501551000     NaN     NaN  MU2261  NaN   1\n",
       "1  PVG  KMG  1126.0  1501544400  1501556400     NaN     NaN  CZ3677  NaN   1\n",
       "2  SJW  NKG   448.0  1501545300  1501551300     NaN     NaN  9C8939  NaN   1\n",
       "3  XIY  NKG   779.0  1501545300  1501552200     NaN     NaN  MU2387  NaN   1\n",
       "4  TAO  PVG   948.0  1501543800  1501548900     NaN     NaN  MU5573  NaN   1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flight_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 时间信息预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 转化成日期格式\n",
    "flight_data['计划起飞时间1'] = pd.to_datetime(flight_data['计划出发时间'],unit='s',utc=True)\n",
    "flight_data['计划到达时间1'] = pd.to_datetime(flight_data['计划到达时间'],unit='s',utc=True)\n",
    "# # 计划飞行时间\n",
    "flight_data['计划飞行时间'] = flight_data['计划到达时间1'] - flight_data['计划起飞时间1']\n",
    "flight_data['计划飞行时间'] = flight_data['计划飞行时间'].apply(lambda x: x.days * 86400 + x.seconds if not(pd.isnull(x)) else None)\n",
    "\n",
    "flight_data['计划飞行时间'] = flight_data['计划飞行时间']/3600  # 转换为小时\n",
    "# 细分时间段\n",
    "flight_data['计划起飞日期'] = flight_data['计划起飞时间1'].apply(lambda x:x.strftime('%Y-%m-%d') if not(pd.isnull(x)) else None)\n",
    "flight_data['计划起飞时刻'] = flight_data['计划起飞时间1'].apply(lambda x:x.strftime('%H') if not(pd.isnull(x)) else None)\n",
    "flight_data['航班月份'] = flight_data['计划起飞时间1'].apply(lambda x:int(x.strftime('%m')) if not(pd.isnull(x)) else None)\n",
    "\n",
    "flight_data['计划到达日期'] = flight_data['计划到达时间1'].apply(lambda x:x.strftime('%Y-%m-%d') if not(pd.isnull(x)) else None)\n",
    "flight_data['计划到达时刻'] = flight_data['计划到达时间1'].apply(lambda x:x.strftime('%H') if not(pd.isnull(x)) else None)\n",
    "##延误\n",
    "flight_data['起飞延误时间'] = pd.to_datetime(flight_data['实际出发时间'],unit='s',utc=True) - pd.to_datetime(flight_data['计划出发时间'],unit='s',utc=True)\n",
    "flight_data['起飞延误时间'] = flight_data['起飞延误时间'].apply(lambda x: x.days * 86400 + x.seconds if not(pd.isnull(x)) else None)\n",
    "flight_data['起飞延误时间'] = flight_data['起飞延误时间']/3600  # 转换为分钟\n",
    "flight_data['起飞延误时间'] = np.where(flight_data['取消标识'] == '取消',10,flight_data['起飞延误时间'])\n",
    "\n",
    "flight_data['到达延误时间'] = pd.to_datetime(flight_data['实际到达时间'],unit='s',utc=True) - pd.to_datetime(flight_data['计划到达时间'],unit='s',utc=True)\n",
    "flight_data['到达延误时间'] = flight_data['到达延误时间'].apply(lambda x: x.days * 86400 + x.seconds if not(pd.isnull(x)) else None)\n",
    "flight_data['到达延误时间'] = flight_data['到达延误时间']/3600  # 转换为分钟\n",
    "flight_data['到达延误时间'] = np.where(flight_data['取消标识'] == '取消',10,flight_data['到达延误时间'])\n",
    "\n",
    "del flight_data['计划起飞时间1']\n",
    "del flight_data['计划到达时间1']\n",
    "del(flight_data['实际出发时间'])\n",
    "del(flight_data['取消标识'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出发机场</th>\n",
       "      <th>到达机场</th>\n",
       "      <th>飞机编号</th>\n",
       "      <th>计划出发时间</th>\n",
       "      <th>计划到达时间</th>\n",
       "      <th>实际到达时间</th>\n",
       "      <th>航班编号</th>\n",
       "      <th>标识</th>\n",
       "      <th>计划飞行时间</th>\n",
       "      <th>计划起飞日期</th>\n",
       "      <th>计划起飞时刻</th>\n",
       "      <th>航班月份</th>\n",
       "      <th>计划到达日期</th>\n",
       "      <th>计划到达时刻</th>\n",
       "      <th>起飞延误时间</th>\n",
       "      <th>到达延误时间</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XIY</td>\n",
       "      <td>CKG</td>\n",
       "      <td>2403.0</td>\n",
       "      <td>1501545000</td>\n",
       "      <td>1501551000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MU2261</td>\n",
       "      <td>1</td>\n",
       "      <td>1.666667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>PVG</td>\n",
       "      <td>KMG</td>\n",
       "      <td>1126.0</td>\n",
       "      <td>1501544400</td>\n",
       "      <td>1501556400</td>\n",
       "      <td>NaN</td>\n",
       "      <td>CZ3677</td>\n",
       "      <td>1</td>\n",
       "      <td>3.333333</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>03</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SJW</td>\n",
       "      <td>NKG</td>\n",
       "      <td>448.0</td>\n",
       "      <td>1501545300</td>\n",
       "      <td>1501551300</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9C8939</td>\n",
       "      <td>1</td>\n",
       "      <td>1.666667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>XIY</td>\n",
       "      <td>NKG</td>\n",
       "      <td>779.0</td>\n",
       "      <td>1501545300</td>\n",
       "      <td>1501552200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MU2387</td>\n",
       "      <td>1</td>\n",
       "      <td>1.916667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>TAO</td>\n",
       "      <td>PVG</td>\n",
       "      <td>948.0</td>\n",
       "      <td>1501543800</td>\n",
       "      <td>1501548900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MU5573</td>\n",
       "      <td>1</td>\n",
       "      <td>1.416667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  出发机场 到达机场    飞机编号      计划出发时间      计划到达时间  实际到达时间    航班编号  标识    计划飞行时间  \\\n",
       "0  XIY  CKG  2403.0  1501545000  1501551000     NaN  MU2261   1  1.666667   \n",
       "1  PVG  KMG  1126.0  1501544400  1501556400     NaN  CZ3677   1  3.333333   \n",
       "2  SJW  NKG   448.0  1501545300  1501551300     NaN  9C8939   1  1.666667   \n",
       "3  XIY  NKG   779.0  1501545300  1501552200     NaN  MU2387   1  1.916667   \n",
       "4  TAO  PVG   948.0  1501543800  1501548900     NaN  MU5573   1  1.416667   \n",
       "\n",
       "       计划起飞日期 计划起飞时刻  航班月份      计划到达日期 计划到达时刻  起飞延误时间  到达延误时间  \n",
       "0  2017-07-31     23     7  2017-08-01     01     NaN     NaN  \n",
       "1  2017-07-31     23     7  2017-08-01     03     NaN     NaN  \n",
       "2  2017-07-31     23     7  2017-08-01     01     NaN     NaN  \n",
       "3  2017-07-31     23     7  2017-08-01     01     NaN     NaN  \n",
       "4  2017-07-31     23     7  2017-08-01     00     NaN     NaN  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flight_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 前序航班的延误时间&到达与起飞间隔\n",
    "这里前序航班的定义为：同一架飞机，当前航班的前一个航班即为当前航班的前序航班。比如，同一架飞机连续飞两个航班A：南京--北京，B：北京--西安，则A为B的前序航班。\n",
    "## 延误时间（单位：h）\n",
    "前序航班的延误时间定义为前序航班到达延误时间，即时间到达时间减去计划到达时间\n",
    "## 到达与起飞间隔（单位：h）\n",
    "当前航班的计划起飞时间与前序航班时间到达时间的间隔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "flight_data['飞机编号']= flight_data['飞机编号'].fillna(0)\n",
    "flight_data['前序延误'] = pd.Series()\n",
    "flight_data['起飞间隔'] = pd.Series()\n",
    "grouped = flight_data.groupby(flight_data['飞机编号'])\n",
    "chunks = []\n",
    "for name,group in grouped:\n",
    "    group = group.sort_values('计划出发时间')\n",
    "    a = pd.to_datetime(group['计划出发时间'],unit='s',utc=True)[1:].reset_index(drop=True)\n",
    "    b = pd.to_datetime(group['实际到达时间'],unit='s',utc=True)[0:len(group)-1].reset_index(drop=True)  \n",
    "    group['起飞间隔'][1:] = a-b\n",
    "    group['起飞间隔'] = group['起飞间隔'].apply(lambda x: x.days * 86400 + x.seconds if not(pd.isnull(x)) else None)\n",
    "    group['起飞间隔'] = group['起飞间隔']/3600\n",
    "    \n",
    "    group['前序延误'][1:] = group['到达延误时间'][0:len(group)-1]\n",
    "    chunks.append(group)\n",
    "flight_data = pd.concat(chunks, ignore_index=True) \n",
    "del(grouped)\n",
    "del(group)\n",
    "del(chunks)\n",
    "flight_data['前序延误'][flight_data['飞机编号']==0] = np.NaN\n",
    "flight_data['起飞间隔'][flight_data['飞机编号']==0] = np.NaN\n",
    "flight_data = flight_data[flight_data['标识']==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出发机场</th>\n",
       "      <th>到达机场</th>\n",
       "      <th>飞机编号</th>\n",
       "      <th>计划出发时间</th>\n",
       "      <th>计划到达时间</th>\n",
       "      <th>实际到达时间</th>\n",
       "      <th>航班编号</th>\n",
       "      <th>标识</th>\n",
       "      <th>计划飞行时间</th>\n",
       "      <th>计划起飞日期</th>\n",
       "      <th>计划起飞时刻</th>\n",
       "      <th>航班月份</th>\n",
       "      <th>计划到达日期</th>\n",
       "      <th>计划到达时刻</th>\n",
       "      <th>起飞延误时间</th>\n",
       "      <th>到达延误时间</th>\n",
       "      <th>前序延误</th>\n",
       "      <th>起飞间隔</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HRB</td>\n",
       "      <td>TNA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542000</td>\n",
       "      <td>1501550400</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC8727</td>\n",
       "      <td>1</td>\n",
       "      <td>2.333333</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HRB</td>\n",
       "      <td>NNG</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542000</td>\n",
       "      <td>1501564200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC8727</td>\n",
       "      <td>1</td>\n",
       "      <td>6.166667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>XMN</td>\n",
       "      <td>HSN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542300</td>\n",
       "      <td>1501547400</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC4967</td>\n",
       "      <td>1</td>\n",
       "      <td>1.416667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>XMN</td>\n",
       "      <td>TAO</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542300</td>\n",
       "      <td>1501555500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC4967</td>\n",
       "      <td>1</td>\n",
       "      <td>3.666667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>KMG</td>\n",
       "      <td>ZUH</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542900</td>\n",
       "      <td>1501549500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8L9893</td>\n",
       "      <td>1</td>\n",
       "      <td>1.833333</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  出发机场 到达机场  飞机编号      计划出发时间      计划到达时间  实际到达时间    航班编号  标识    计划飞行时间  \\\n",
       "0  HRB  TNA   0.0  1501542000  1501550400     NaN  SC8727   1  2.333333   \n",
       "1  HRB  NNG   0.0  1501542000  1501564200     NaN  SC8727   1  6.166667   \n",
       "2  XMN  HSN   0.0  1501542300  1501547400     NaN  SC4967   1  1.416667   \n",
       "3  XMN  TAO   0.0  1501542300  1501555500     NaN  SC4967   1  3.666667   \n",
       "4  KMG  ZUH   0.0  1501542900  1501549500     NaN  8L9893   1  1.833333   \n",
       "\n",
       "       计划起飞日期 计划起飞时刻  航班月份      计划到达日期 计划到达时刻  起飞延误时间  到达延误时间  前序延误 起飞间隔  \n",
       "0  2017-07-31     23     7  2017-08-01     01     NaN     NaN   NaN  NaN  \n",
       "1  2017-07-31     23     7  2017-08-01     05     NaN     NaN   NaN  NaN  \n",
       "2  2017-07-31     23     7  2017-08-01     00     NaN     NaN   NaN  NaN  \n",
       "3  2017-07-31     23     7  2017-08-01     02     NaN     NaN   NaN  NaN  \n",
       "4  2017-07-31     23     7  2017-08-01     01     NaN     NaN   NaN  NaN  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flight_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 特情\n",
    "特情这个特征的处理，没有区分特情的具体内容，只将特情发生的时间段对应到计划起飞和到达的时间，以0代表没有发生特情，1表示发生了特情，所以后面有继续优化这个特征的空间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# --------------------------------------------------特情处理------------------------------------------#\n",
    "del(spcial['采集时间'])\n",
    "del(spcial['特情'])\n",
    "spcial['开始日期'] = spcial['开始时间'].apply(lambda x : x.strftime('%F'))\n",
    "spcial['开始时刻'] = spcial['开始时间'].apply(lambda x : x.strftime('%H'))\n",
    "spcial['结束时刻'] = spcial['结束时间'].apply(lambda x : x.strftime('%H'))\n",
    "\n",
    "del(spcial['开始时间'])\n",
    "del(spcial['结束时间'])\n",
    "spcial = spcial.drop_duplicates(['机场','开始日期'])\n",
    "flight_data = pd.merge(flight_data,spcial,left_on=['到达机场','计划到达日期'],right_on=['机场','开始日期'],how='left',sort=False)\n",
    "\n",
    "flight_data['到达特情'] = np.where((flight_data['计划到达时刻'] >=flight_data['开始时刻']) &\n",
    "                               (flight_data['计划到达时刻']<= flight_data['结束时刻']),1,0)\n",
    "del(flight_data['机场'])\n",
    "del(flight_data['开始日期'] )\n",
    "del(flight_data['开始时刻'])\n",
    "del(flight_data['结束时刻'] )\n",
    "flight_data = pd.merge(flight_data,spcial,left_on=['出发机场','计划起飞日期'],right_on=['机场','开始日期'],how='left',sort=False)\n",
    "\n",
    "flight_data['出发特情'] = np.where((flight_data['计划起飞时刻'] >=flight_data['开始时刻']) &\n",
    "                               (flight_data['计划起飞时刻']<= flight_data['结束时刻']),1,0)\n",
    "del(flight_data['机场'])\n",
    "del(flight_data['开始日期'] )\n",
    "del(flight_data['开始时刻'])\n",
    "del(flight_data['结束时刻'] )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 天气\n",
    "天气特征的提取主要包括气温特征和天气情况，其中：\n",
    "\n",
    "1. 气温划分为3个取值，大于40度为高温，小于-10度为低温，其他为一般\n",
    "2. 天气情况（小雨、阴天等）根据组织方提供的天气信息文件，两年时间内，所有时间所有地点出现频率小于50的天气统一划归为‘other’"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # ———————————————————————————天气数据预处理———————————————————— #\n",
    "weather['日期'] = weather['日期'].apply(lambda x : x.strftime('%F'))\n",
    "weather['气温'] = pd.Series()\n",
    "weather['最高气温'] = weather['最高气温'].fillna('0')\n",
    "weather['最低气温'] = weather['最低气温'].fillna('0')\n",
    "weather['气温'] = weather['最高气温'].apply(lambda x: '高温' if int(x)>=40 else '一般')\n",
    "weather['气温'] = np.where(weather['最低气温'].astype('int') < -10,'低温',weather['气温'])\n",
    "del(weather['最高气温'])\n",
    "del(weather['最低气温'])\n",
    "weather = weather.drop_duplicates() # 只包含这三个字段\n",
    "\n",
    "weather_case = list(case['0'])\n",
    "weather['天气'] = weather['天气'].apply(lambda x: x if x in set(weather_case) else 'other')\n",
    "# # 将机场编码对应到天气数据上面，根据城市名\n",
    "airport_weather = pd.merge(weather,airport_city,left_on=['城市'],right_on=['城市名称'],how='left',sort=False)\n",
    "del(airport_weather['城市名称'])\n",
    "# 去除缺失值和重复的机场天气信息\n",
    "airport_weather = airport_weather.dropna()\n",
    "airport_weather = airport_weather.drop_duplicates(['日期','机场编码'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>城市</th>\n",
       "      <th>天气</th>\n",
       "      <th>日期</th>\n",
       "      <th>气温</th>\n",
       "      <th>机场编码</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>忻州</td>\n",
       "      <td>阵雨</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>一般</td>\n",
       "      <td>WUT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>大连</td>\n",
       "      <td>多云转阵雨</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>一般</td>\n",
       "      <td>DLC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>汉中</td>\n",
       "      <td>晴</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>一般</td>\n",
       "      <td>HZG</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>潍坊</td>\n",
       "      <td>阵雨转多云</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>一般</td>\n",
       "      <td>WEF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>博乐</td>\n",
       "      <td>晴</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>一般</td>\n",
       "      <td>BPL</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    城市     天气          日期  气温 机场编码\n",
       "17  忻州     阵雨  2017-08-01  一般  WUT\n",
       "26  大连  多云转阵雨  2017-08-01  一般  DLC\n",
       "30  汉中      晴  2017-08-01  一般  HZG\n",
       "33  潍坊  阵雨转多云  2017-08-01  一般  WEF\n",
       "35  博乐      晴  2017-08-01  一般  BPL"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "airport_weather.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 将天气匹配到航班动态表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 出发城市\n",
    "flight_data = pd.merge(flight_data,airport_weather,left_on=['出发机场','计划起飞日期'],right_on=['机场编码','日期'],how='left',sort=False)\n",
    "flight_data['出发天气'] = flight_data['天气']\n",
    "flight_data['出发气温'] = flight_data['气温']\n",
    "del(flight_data['天气'])\n",
    "del(flight_data['机场编码'])\n",
    "del(flight_data['城市'])\n",
    "del[flight_data['日期']]\n",
    "del(flight_data['气温'])\n",
    "# 到达城市\n",
    "flight_data = pd.merge(flight_data,airport_weather,left_on=['到达机场','计划到达日期'],right_on=['机场编码','日期'],how='left',sort=False)\n",
    "flight_data['到达天气'] = flight_data['天气']\n",
    "flight_data['到达气温'] = flight_data['气温']\n",
    "del(flight_data['天气'])\n",
    "del(flight_data['机场编码'])\n",
    "del(flight_data['城市'])\n",
    "del[flight_data['日期']]\n",
    "del(flight_data['气温'])\n",
    "del(flight_data['实际到达时间'])\n",
    "del(flight_data['计划起飞日期'])\n",
    "del(flight_data['到达延误时间'])\n",
    "del(flight_data['计划到达日期'])\n",
    "del(flight_data['标识'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出发机场</th>\n",
       "      <th>到达机场</th>\n",
       "      <th>飞机编号</th>\n",
       "      <th>计划出发时间</th>\n",
       "      <th>计划到达时间</th>\n",
       "      <th>实际到达时间</th>\n",
       "      <th>航班编号</th>\n",
       "      <th>标识</th>\n",
       "      <th>计划飞行时间</th>\n",
       "      <th>计划起飞日期</th>\n",
       "      <th>...</th>\n",
       "      <th>起飞间隔</th>\n",
       "      <th>到达特情</th>\n",
       "      <th>出发特情</th>\n",
       "      <th>城市</th>\n",
       "      <th>天气</th>\n",
       "      <th>最低气温</th>\n",
       "      <th>最高气温</th>\n",
       "      <th>日期</th>\n",
       "      <th>机场编码</th>\n",
       "      <th>出发天气</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HRB</td>\n",
       "      <td>TNA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542000</td>\n",
       "      <td>1501550400</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC8727</td>\n",
       "      <td>1</td>\n",
       "      <td>2.333333</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HRB</td>\n",
       "      <td>NNG</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542000</td>\n",
       "      <td>1501564200</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC8727</td>\n",
       "      <td>1</td>\n",
       "      <td>6.166667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>XMN</td>\n",
       "      <td>HSN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542300</td>\n",
       "      <td>1501547400</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC4967</td>\n",
       "      <td>1</td>\n",
       "      <td>1.416667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>XMN</td>\n",
       "      <td>TAO</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542300</td>\n",
       "      <td>1501555500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>SC4967</td>\n",
       "      <td>1</td>\n",
       "      <td>3.666667</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>KMG</td>\n",
       "      <td>ZUH</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542900</td>\n",
       "      <td>1501549500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8L9893</td>\n",
       "      <td>1</td>\n",
       "      <td>1.833333</td>\n",
       "      <td>2017-07-31</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaT</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  出发机场 到达机场  飞机编号      计划出发时间      计划到达时间  实际到达时间    航班编号  标识    计划飞行时间  \\\n",
       "0  HRB  TNA   0.0  1501542000  1501550400     NaN  SC8727   1  2.333333   \n",
       "1  HRB  NNG   0.0  1501542000  1501564200     NaN  SC8727   1  6.166667   \n",
       "2  XMN  HSN   0.0  1501542300  1501547400     NaN  SC4967   1  1.416667   \n",
       "3  XMN  TAO   0.0  1501542300  1501555500     NaN  SC4967   1  3.666667   \n",
       "4  KMG  ZUH   0.0  1501542900  1501549500     NaN  8L9893   1  1.833333   \n",
       "\n",
       "       计划起飞日期  ...  起飞间隔  到达特情 出发特情   城市   天气  最低气温  最高气温  日期  机场编码  出发天气  \n",
       "0  2017-07-31  ...   NaN     0    0  NaN  NaN   NaN   NaN NaT   NaN   NaN  \n",
       "1  2017-07-31  ...   NaN     0    0  NaN  NaN   NaN   NaN NaT   NaN   NaN  \n",
       "2  2017-07-31  ...   NaN     0    0  NaN  NaN   NaN   NaN NaT   NaN   NaN  \n",
       "3  2017-07-31  ...   NaN     0    0  NaN  NaN   NaN   NaN NaT   NaN   NaN  \n",
       "4  2017-07-31  ...   NaN     0    0  NaN  NaN   NaN   NaN NaT   NaN   NaN  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flight_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 航空公司"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 航班性质为：0：补飞，1：国内正常，2：国外\n",
    "flight_data['航空公司'] = flight_data['航班编号'].apply(lambda x : x[:2])\n",
    "def f(x):\n",
    "    if x[-1].isalpha():\n",
    "        y = 0\n",
    "    elif len(x[2:]) == 4:\n",
    "        y = 1\n",
    "    else:\n",
    "        y = 2\n",
    "    return(y)\n",
    "flight_data['航班性质'] = flight_data['航班编号'].apply(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>出发机场</th>\n",
       "      <th>到达机场</th>\n",
       "      <th>飞机编号</th>\n",
       "      <th>计划出发时间</th>\n",
       "      <th>计划到达时间</th>\n",
       "      <th>航班编号</th>\n",
       "      <th>计划飞行时间</th>\n",
       "      <th>计划起飞时刻</th>\n",
       "      <th>航班月份</th>\n",
       "      <th>计划到达时刻</th>\n",
       "      <th>...</th>\n",
       "      <th>前序延误</th>\n",
       "      <th>起飞间隔</th>\n",
       "      <th>到达特情</th>\n",
       "      <th>出发特情</th>\n",
       "      <th>出发天气</th>\n",
       "      <th>出发气温</th>\n",
       "      <th>到达天气</th>\n",
       "      <th>到达气温</th>\n",
       "      <th>航空公司</th>\n",
       "      <th>航班性质</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HRB</td>\n",
       "      <td>TNA</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542000</td>\n",
       "      <td>1501550400</td>\n",
       "      <td>SC8727</td>\n",
       "      <td>2.333333</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>01</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>雷阵雨</td>\n",
       "      <td>一般</td>\n",
       "      <td>SC</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HRB</td>\n",
       "      <td>NNG</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542000</td>\n",
       "      <td>1501564200</td>\n",
       "      <td>SC8727</td>\n",
       "      <td>6.166667</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>05</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>阵雨</td>\n",
       "      <td>一般</td>\n",
       "      <td>SC</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>XMN</td>\n",
       "      <td>HSN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542300</td>\n",
       "      <td>1501547400</td>\n",
       "      <td>SC4967</td>\n",
       "      <td>1.416667</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>00</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>阵雨转阴</td>\n",
       "      <td>一般</td>\n",
       "      <td>SC</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>XMN</td>\n",
       "      <td>TAO</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542300</td>\n",
       "      <td>1501555500</td>\n",
       "      <td>SC4967</td>\n",
       "      <td>3.666667</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>02</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>阵雨</td>\n",
       "      <td>一般</td>\n",
       "      <td>SC</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>KMG</td>\n",
       "      <td>ZUH</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1501542900</td>\n",
       "      <td>1501549500</td>\n",
       "      <td>8L9893</td>\n",
       "      <td>1.833333</td>\n",
       "      <td>23</td>\n",
       "      <td>7</td>\n",
       "      <td>01</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>阵雨转多云</td>\n",
       "      <td>一般</td>\n",
       "      <td>8L</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  出发机场 到达机场  飞机编号      计划出发时间      计划到达时间    航班编号    计划飞行时间 计划起飞时刻  航班月份  \\\n",
       "0  HRB  TNA   0.0  1501542000  1501550400  SC8727  2.333333     23     7   \n",
       "1  HRB  NNG   0.0  1501542000  1501564200  SC8727  6.166667     23     7   \n",
       "2  XMN  HSN   0.0  1501542300  1501547400  SC4967  1.416667     23     7   \n",
       "3  XMN  TAO   0.0  1501542300  1501555500  SC4967  3.666667     23     7   \n",
       "4  KMG  ZUH   0.0  1501542900  1501549500  8L9893  1.833333     23     7   \n",
       "\n",
       "  计划到达时刻 ...   前序延误  起飞间隔 到达特情  出发特情  出发天气 出发气温   到达天气 到达气温 航空公司 航班性质  \n",
       "0     01 ...    NaN   NaN    0     0   NaN  NaN    雷阵雨   一般   SC    1  \n",
       "1     05 ...    NaN   NaN    0     0   NaN  NaN     阵雨   一般   SC    1  \n",
       "2     00 ...    NaN   NaN    0     0   NaN  NaN   阵雨转阴   一般   SC    1  \n",
       "3     02 ...    NaN   NaN    0     0   NaN  NaN     阵雨   一般   SC    1  \n",
       "4     01 ...    NaN   NaN    0     0   NaN  NaN  阵雨转多云   一般   8L    1  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flight_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 保存处理完的测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 注意这个处理完的数据集保存在了当前文件夹下-----‘处理后测试集’\n",
    "os.chdir(\"处理后测试集\")\n",
    "flight_data.to_csv('test_data1.csv',index=False) \n",
    "os.chdir(pwd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
